mainMenu = {}

--See if there is a saved high score and load it
--Create one if there isn't
if love.filesystem.exists("high.score") then
  highScore = love.filesystem.read("high.score")
  --Make sure that it's formatted as a number
  highScore = tonumber(highScore)
  --If not, reset score
  if type(highScore) ~= "number" then
    highScore = 0
  end
  
  formatHighScore = (tostring(highScore):reverse():gsub("%d%d%d", "%1,"):reverse():gsub("^,", ""))
else
  highScore = 0
end

local menuSelection = 0
local menuMusic
local titleFont = love.graphics.newFont('assets/Science Fair.otf', 120)
background = love.graphics.newImage('assets/textures/background1.png')
back2 = love.graphics.newImage('assets/textures/background2.png')
back3 = love.graphics.newImage('assets/textures/background3.png')

function mainMenu:init()
  menuOptions = {}
  menuOptions[1] = {text = "Play", x = 640 - hudFontLarge:getWidth("Play")/2, y = 260, selected = false}
  menuOptions[2] = {text = "Help", x = 640 - hudFontLarge:getWidth("Help")/2, y = 400, selected = false}
  menuOptions[3] = {text = "Exit", x = 640 - hudFontLarge:getWidth("Exit")/2, y = 540, selected = false}
  
  menuMusic = love.audio.newSource('assets/sounds/ADistantFuture.ogg')
  
end

function mainMenu:enter()
  formatHighScore = (tostring(highScore):reverse():gsub("%d%d%d", "%1,"):reverse():gsub("^,", ""))
  
  love.audio.stop()
  menuSelection = 0
  love.audio.play(menuMusic)
  
end

function mainMenu:update(dt)
   --Which option is selected?
  if menuSelection == 0 then
    menuOptions[1].selected = true
    menuOptions[2].selected = false
    menuOptions[3].selected = false
  elseif menuSelection == 1 then
    menuOptions[1].selected = false
    menuOptions[2].selected = true
    menuOptions[3].selected = false
  elseif menuSelection == 2 then
    menuOptions[1].selected = false
    menuOptions[2].selected = false
    menuOptions[3].selected = true
  end
end

function mainMenu:draw(dt)
  --Background
  love.graphics.setColor(color.white)
  love.graphics.draw(background)
  love.graphics.setColor(90,90,90,255)
  love.graphics.draw(back2)
  love.graphics.setColor(54,54,54,255)
  love.graphics.draw(back3)
  
  --Menu Title
  love.graphics.setColor(color.white)
  love.graphics.setFont(titleFont)
  love.graphics.print("Asteroids", 640 - titleFont:getWidth("Asteroids")/2, 50)
  
  --Sophisticated Sparrow Gaming
  love.graphics.setFont(hudFontSmall)
  love.graphics.print("(c) 2013 Sophisticated Sparrow Gaming", 640 - hudFontSmall:getWidth("2013 Sophisticated Sparrow Gaming")/2, 720 - 20 - hudFont:getHeight("2013 Sophisticated Sparrow Gaming"))
  
  --Draw the menu with the appropriate options
  love.graphics.setFont(hudFontLarge)
  for i = 1, #menuOptions do
    --Set colors for appropriate menu options
    if menuOptions[i].selected then
      love.graphics.setColor(color.green)
    else
      love.graphics.setColor(color.red)
    end
    --Draw the menu text
    love.graphics.print(menuOptions[i].text, menuOptions[i].x, menuOptions[i].y)
  end
  
  --Draw toggle for audio
  love.graphics.setFont(hudFontSmall)
  love.graphics.setColor(color.red)
  love.graphics.print("[M]ute Audio", 1280-20-hudFontSmall:getWidth("[M]ute Audio"), 720 - 20 - hudFontSmall:getHeight("[M]ute Audio"))
  
  --Reset high score
  love.graphics.print("[R]eset High Score", 1280-20-hudFontSmall:getWidth("[R]eset High Score"), 720 - 20 - hudFontSmall:getHeight("[M]ute Audio") - 20 - hudFontSmall:getHeight("[R]eset High Score"))
  
  --Draw toggle for fullscreen
  love.graphics.print("[F]ullscreen", 20, 720-20-hudFontSmall:getHeight("[F]ullscreen"))
  
  love.graphics.setColor(color.white)
  local t = "High Score: "..formatHighScore
  love.graphics.print(t, 640 - hudFontSmall:getWidth(t)/2, 180)
end

function mainMenu:leave()
  love.audio.stop()
end

--Input
function mainMenu:keypressed(k)
  --Change menu option
  if k == "w" or k == "up" then
    menuSelection = menuSelection - 1
  elseif k == "s" or k == "down" then
    menuSelection = menuSelection + 1
  end

  --Cycle through menu options
  if menuSelection > 2 then 
    menuSelection = 0
  elseif menuSelection < 0 then
    menuSelection = 2
  end
  
  --Mute/Unmute audio
  if k == "m" then
    if love.audio.getVolume() ~= 1 then
      love.audio.setVolume(1)
    else
      love.audio.setVolume(0)
    end
  end
  
  --Reset high score
  if k == 'r' then
    highScore = 0
    formatHighScore = 0
    love.filesystem.write("high.score", highScore)
  end
  
  
  --Toggle Fullscreen
  if k == 'f' then
    love.graphics.toggleFullscreen()
  end
  
  
  --Select Option
  if k == 'return' then
    if menuSelection == 0 then
      Gamestate.switch(mainGame)
    elseif menuSelection == 1 then
      Gamestate.push(helpMenu)
    elseif menuSelection == 2 then
      love.event.quit()
  end
    
  end
end
function mainMenu:keyreleased(k)
end
function mainMenu:mousepressed(x,y,b)
end
function mainMenu:mousereleased(x,y,b)
end
